package com.gzy.mylog;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gzy.mylog.manager.AuthManager;
import com.gzy.mylog.model.Result;
import com.gzy.mylog.model.User;
import com.gzy.mylog.net.core.XXModelCallback;
import com.gzy.mylog.net.net.ApiCallback;
import com.gzy.mylog.net.AuthApi;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.BufferedSink;

import static com.gzy.mylog.net.NetConfig.BASE_URL;

public class LoginActivity extends BaseActivity {


    EditText edt_pwd;
    EditText edt_username;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findViewById(R.id.btn_login).setOnClickListener((v)->{
            login();
        });

        edt_pwd = findViewById(R.id.edt_pwd);
        edt_username = findViewById(R.id.edt_username);
        // >lxf<


        findViewById(R.id.tv_jumpSignup).setOnClickListener((v) -> {
            startActivity(new Intent(this,SignupActivity.class));
        });

    }

    void login(){
        String edtPwdText = edt_pwd.getText().toString().trim();
        String edtUsernameText = edt_username.getText().toString().trim();

        // 数据的校验 || edtPwdText.length() > 3
        if (TextUtils.isEmpty(edtUsernameText) ){
            Toast.makeText(this, "请输入合法的用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(edtPwdText)  || edtPwdText.length() < 6 ){
            Toast.makeText(this, "请输入合适的密码", Toast.LENGTH_SHORT).show();
            return;
        }

        AuthApi.login(edtUsernameText, edtPwdText, new XXModelCallback<Result<String>>(new TypeReference<Result<String>>(){}) {
            @Override
            public void onResponseData(Call call, Result<String> model) {
                System.out.println(model);
                String token = model.getData();
                loadUserInfo(token);
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(LoginActivity.this, "登录失败:"+message  ,Toast.LENGTH_SHORT).show();
            }
        });

    }

    void loadUserInfo(String token){
        AuthApi.loadUserInfo(token, new XXModelCallback<Result<User>>(new TypeReference<Result<User>>(){}) {
            @Override
            public void onResponseData(Call call, Result<User> model) {
                AuthManager.saveLoginState(LoginActivity.this,token,model.getData());
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);

                runOnUiThread(()->{
                    finish();
                },300);
            }

            @Override
            public void onFailure2(Call call, IOException e, ErrType type, String message) {
                Toast.makeText(LoginActivity.this, "登录失败:"+message  ,Toast.LENGTH_SHORT).show();
            }
        });
    }
}
